package com.pyp.kuaishou.repository;

import com.pyp.kuaishou.model.StorePlatformConfig;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
 * 作者: lauJinyu
 * 仓储层：严格避免Controller直接操作数据库
 */
@Repository
public class StorePlatformConfigRepository {

    private final NamedParameterJdbcTemplate jdbcTemplate;

    public StorePlatformConfigRepository(NamedParameterJdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public Optional<StorePlatformConfig> findByAppId(String appId) {
        // 仅查询基础配置字段，不再包含config_data
        String sql = "SELECT id, store_id AS storeId, app_id AS appId, app_secret AS appSecret, created_at AS createdAt, updated_at AS updatedAt FROM store_platform_configs WHERE app_id = :appId LIMIT 1";
        MapSqlParameterSource params = new MapSqlParameterSource("appId", appId);
        return jdbcTemplate.query(sql, params, new BeanPropertyRowMapper<>(StorePlatformConfig.class))
                .stream().findFirst();
    }

    // 已移除updateConfigData方法：取消任何持久化token或临时配置的写入
}